package com.jf.net;

import android.util.Pair;

import com.jf.net.converter.GsonConverterFactory;
import com.jf.net.utils.GsonUtil;
import com.jf.net.utils.SSLFactoryUtil;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.CallAdapter.Factory;
import retrofit2.Converter;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * FileDescription
 * Created by kai.zhang.MS-1
 * 12/01/2022
 */
public class NetworkApi {
    private static final NetServiceHelper netServiceHelper = NetServiceHelper.Companion.getInstance();

    private NetworkApi() {
    }

    public static <T> T create(String baseUrl,
                               List<Interceptor> interceptors,
                               Pair<SSLSocketFactory, X509TrustManager> sslSocketManager,
                               Class<T> tClass) {
        return create(baseUrl, interceptors, sslSocketManager, tClass, (Proxy)null);
    }

    public static <T> T create(String baseUrl,
                               List<Interceptor> interceptors,
                               Pair<SSLSocketFactory, X509TrustManager> sslSocketManager,
                               Class<T> tClass,
                               List<Factory> adapters) {

        return create(baseUrl, interceptors, sslSocketManager, tClass, null, adapters);
    }

    public static <T> T create(String baseUrl,
                               List<Interceptor> interceptors,
                               Pair<SSLSocketFactory, X509TrustManager> sslSocketManager,
                               Class<T> tClass,
                               Proxy proxy) {
        return create(baseUrl, interceptors, sslSocketManager, tClass, proxy, null);
    }

    public static <T> T create(String baseUrl,
                               List<Interceptor> interceptors,
                               Pair<SSLSocketFactory, X509TrustManager> sslSocketManager,
                               Class<T> tClass, Proxy proxy,
                               List<Factory> adapters) {
        try {
            List<Converter.Factory> converters = new ArrayList<>();
            converters.add(ScalarsConverterFactory.create());
            converters.add(GsonConverterFactory.create(GsonUtil.gson));
            OkHttpClient.Builder builder = netServiceHelper
                    .getOkHttpClient(interceptors, sslSocketManager, 1000);
            if (proxy != null) {
                builder.proxy(proxy);
            }
            if(adapters != null && !adapters.isEmpty()){
                return netServiceHelper.create(baseUrl, tClass, converters, adapters, builder);
            }else{
                return netServiceHelper.create(baseUrl, tClass, converters, builder);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 根据路径自定义配置ssl证书
     *
     * @param rootCrtFilePath root证书
     * @param clientCrtFilePath client证书
     * @param keyFilePath key文件
     * @return Pair<SSLSocketFactory, X509TrustManager>
     */
    public static Pair<SSLSocketFactory, X509TrustManager> getSSLSocketManager(String rootCrtFilePath, String clientCrtFilePath, String keyFilePath) {
        return SSLFactoryUtil.getSSLSocketFactory(rootCrtFilePath, clientCrtFilePath, keyFilePath);
    }

    /**
     * 当从车机上获取到空证书时，使用默认的ssl socket
     *
     * @return default ssl socket with null certificate
     */
    public static Pair<SSLSocketFactory, X509TrustManager> getDefaultSSLSocketManager() {
        return SSLFactoryUtil.getSSLSocketFactory("CS_ROOT_CRT_PATH", "CLIENT_BKS_PATH", "HTTP_PRIVATE_KEY");
    }

}
